/*
 * @Author: WangZhongDe
 * @Date: 2021-07-22 22:00:28
 * @LastEditors: WangZhongDe
 * @LastEditTime: 2021-08-27 11:03:48
 * @FilePath: \rust\learn1\src\main.rs
 */
use rand::Rng;
fn main() {
    // let x:i32 = 1;
    // let x = x + 2_000;
    // println!("Hello, world!{}",x);
    // // Tuple创建
    // let tup:(i32,f64,u8) = (500,6.4,1);
    // let (x,y,z) = tup;
    // println!("{},{},{}",x,y,z);
    // // Tuple元素访问
    // let tup:(i32,f64,u8)=(500,6.4,1);
    // println!("{},{},{}",tup.0,tup.1,tup.2);
    // //函数体中的语句与表达式
    // let y = {  //这是块的表达式,如果在3后面加上分号,就变成语句块就会报错
    //     let x = 1;
    //     x + 3  //此处如果加了分号就分报错
    //   };
    //   println!("The value of y is:{}",y);
    // if语句
    let number:i32 = 5;
    if number < 3 {
       println!("condition was true");
    }else{
      println!("condition was false");
    }
    //loop语句
    let mut counter = 0;
    let result = loop{
      counter += 1;
      if counter == 10 {
        break counter * 2;
      }
    };
    println!("Then result is:{}",result);
    //while循环
    let mut number = 3;
    while number != 0 {
      println!("{}!",number);
      number = number - 1;
    }
    println!("LIFTOFF!!!");
    //for 循环遍历数组
    let a = [10,20,30,40,50];
    for element in a.iter(){
      println!("Then value is:{}",element);
    }
    // for Range倒计数
    for el in(1..10).rev(){
      println!("{}!",el);
    }
   println!("LIFIOFF!");
  //  String类型的数据是存放在heap上的
  let mut s = String::from("hello");
      s.push_str(",world!!!");
      println!("{}",s);  
      // 浅拷贝
  let a = String::from("aaaa");
  let b = a; //此处a赋值给了b,a就失效了,如果不想让a失效就应该写b=a.clone(),即深拷贝,也叫变量克隆
  println!("{}",b);
  // println!("{}",a);  a失效    

  //将值传递给函数将发生移动或复制
  let ss = String::from("Hello world");
  take_ownership(ss); //s进入函数后失效,在这行后不可用了,因为移动s
  let x = 5;
  makes_copy(x); //xf进入函数后还是可用的,因为是复制的x
  println!("x:{}",x);
   //实例化一个长方形
   let rg1 = Rectangle{
    width:30,
    heigth:50,
  };
 let rg1_area =  area(&rg1); //计算面积
println!("rg1的面积是:{}",rg1_area);
println!("{:#?}",rg1);
// 调用结构里的方法
println!("{}",rg1.area());
let mut rng =  rand::thread_rng();
let n:u8  = rng.gen();
println!("rand:{}",n);

}

fn take_ownership(some_string:String){
  println!("{}",some_string)
}
  
fn makes_copy(some_number:i32){
  println!("{}",some_number);
}
// 计算长方形面积
//1、声明一个结构体存放长和宽
#[derive(Debug)]
struct Rectangle{
  width:u32,
  heigth:u32,
}
fn area(rect:&Rectangle)->u32{
  rect.width * rect.heigth
}
// 把方法放入结构体中,使用impl关键字
impl Rectangle{
  fn area(&self)->u32{
    self.width * self.heigth
  }
}
